/*搜索功能通用js*/

$(function(){
    //下拉多选点击事件
    $(".search-parts").on("click",".mutiselectDiv",function(e){
        e.stopPropagation();
        $(this).children(".mutiselect-value").toggle();
    });
    $(document).on('click',function(){
        $('.mutiselect-value').hide();
    });
    $(".search-parts").on("click",".mutiselect-value",function(e){
        e.stopPropagation();
    });
    //向上隐藏
    $(".list-arrow-top").click(function(){
        $(".searchFilter").slideUp(200);
        $(this).hide();
        $(".list-arrow-bottom").css("display","block");
    });
    //向下显示
    $(".list-arrow-bottom").click(function(){
        $(".searchFilter").slideDown(200);
        $(this).hide();
        $(".list-arrow-top").css("display","block");
    });
});

/**
 * 列表通用搜索功能(xiaogq)
 * @param     bootstrapTable       bootstrap表格domId
 * @param     searchFilter         搜索条件页面div DOM Id
 * @param     node          当前节点
 */
function search(bootstrapTable,searchFilter,conditionDiv,node){
    if(bootstrapTable == undefined){
        bootstrapTable = "bootstrapTable";
    }
    if(searchFilter == undefined){
        searchFilter = "searchFilter";
    }
    //获取实体id
    var entityId = $(node).attr("tableCode");
    //获得已经布局的字段信息
    var fields = $(node).attr("fields");
    var mutilSearch = "["+getSearchCondition(searchFilter,conditionDiv,"t_"+entityId)+"]";
    //alert("entityId==="+entityId+"   &fields===="+fields);
    //合并搜索条件
    var condition = {tableCode : entityId,fields : fields,mutilSearch : mutilSearch};
    //重新刷新bootstrap表格
    flashBootstrapTable(bootstrapTable,condition);
}

/**
 * 获取搜索条件
 * @param     searchFilter         搜索条件页面div DOM Id
 * @param     tableName            表名
 * @author    xiaogq
 */
function getSearchCondition(searchFilter,conditionDiv,tableName){
    if(searchFilter == undefined){
        searchFilter = "searchFilter";
    }
    var searchConditions = "", filterFieldArr = new Array();
    var condSpecialArr = ['isnull','notnull','modulenull','modulenotnull','ral_no_x','ral_no_f'];

    //获得用户输入搜索条件
    $("#"+searchFilter+" #"+conditionDiv+" td[name^='field-']").each(function(){
        var fieldname = $(this).attr("name").split("-")[1];
        var fieldHtmlId = tableName+"__"+fieldname;
        //如果是弹窗
        if(searchFilter.indexOf("dialogQuote") == 0 && searchFilter.indexOf("dialogQuotesys") != 0){
            fieldHtmlId = searchFilter+"__"+fieldname;
        }

        var inputvalue = "", compare = $("#"+searchFilter+" #conditon-"+fieldname+" option:selected").val();
        if( compare == 'between' ){
            var inputvalueL = $.trim($("#"+searchFilter+" #"+fieldHtmlId).val());
            var inputvalueR = $.trim($("#"+searchFilter+" #"+fieldHtmlId+"-r").val());
            if( inputvalueL && inputvalueR ){
                inputvalue = inputvalueL +"_"+ inputvalueR;
            }
        }else if( $.inArray(compare,condSpecialArr) > -1 ){
            inputvalue = "null";
        }else{
            inputvalue = $.trim($("#"+searchFilter+" #"+fieldHtmlId).val());
        }

        if( inputvalue ){
            filterFieldArr.push(fieldname);
            searchConditions += "{name: "+fieldname+", condition: "+compare+", value: '"+inputvalue+"'},";
        }
    });
    //console.log("filter ==== "+searchConditions.substring(0,searchConditions.length-1));
    return searchConditions.substring(0,searchConditions.length-1);
}

/**
 * 点击过滤条件中比较下拉框中的“区间”选项添加输入框
 * @author  xiaogq
 */
function appendFilterInputBox(node){
    var $this = $(node);
    var thisTd = $this.parents("td");
    var nextTd = thisTd.next("td")[0];
    var nodeVal = $this.val();

    var condSpecialArr = ['isnull','notnull','modulenull','modulenotnull','ral_no_x','ral_no_f'];
    if( $.inArray(nodeVal,condSpecialArr) > -1 ){
        $(nextTd).find("input,select").val("").attr("disabled",true);
        $(nextTd).find("span.filter-search").hide();
    }else{
        $(nextTd).find("input,select").attr("disabled",false);
        $(nextTd).find("span.filter-search").show();
        if( nodeVal == 'between' ){
            $(nextTd).find(".left-filter-input").removeClass("filter-input").addClass("filter-input1");
            $(nextTd).find(".rigth-filter-input").show();
        }else{
            $(nextTd).find(".left-filter-input").removeClass("filter-input").removeClass("filter-input1").addClass("filter-input");
            $(nextTd).find(".rigth-filter-input").val("").hide();
        }
    }
}

/**
 * 定制查询
 * @author  xiaogq
 * @param     bootstrapTable       bootstrap表格domId
 * @param     searchFilter         搜索条件页面div DOM Id
 * @param    ifSetConditions    是否重新设置bootatrapTable过滤条件（true:是，false：否）
 */
function customQuery(node, bootstrapTable,searchFilter,ifSetConditions){
    if(bootstrapTable == undefined){
        bootstrapTable = "bootstrapTable";
    }
    if(searchFilter == undefined){
        searchFilter = "searchFilter";
    }
    //获取定制查询值
    var thisQueryid = $(node).val();
    var tableCode = $(node).attr("tableCode");
    if(thisQueryid == 'querySaveAs'){//查询另存为
        if(ifSetConditions){
            querySaveAs(tableCode, flexTable, searchFilter);
        }
    }else if(thisQueryid == 'myQuery'){//我的查询
        if(ifSetConditions){
            var menuTitle = $(node + " option:selected").val();
            myQuery(menuTitle, tableCode);
        }
    }else{
        if(thisQueryid != ""){
            //获得列表已经输入的搜索条件
            var searchConditions = "";
            var filterSearch = getSearchCondition(searchFilter,"t_"+tableCode);
            if(filterSearch != ""){
                searchConditions = filterSearch +",";
            }
            //添加定制条件
            if(thisQueryid == 1){		//全部
                document.getElementById("filterForm").reset();
                $("#searchFilter").find("input,select").attr("disabled",false);
                $("#searchFilter").find("span.filter-search").show();
                $("#searchFilter").find(".left-filter-input").removeClass("filter-input1").addClass("filter-input");
                $("#searchFilter").find(".rigth-filter-input").val("").hide();
            }else if(thisQueryid == 2){	//今日新增
                searchConditions += "{name:create_date_datetime,condition: gt, value: '"+ getCurrentDate() +"'},";
            }else if(thisQueryid == 3){	//本周新增
                var firstOfWeek = getFirstDateOfWeek(new Date()).getDate();
                var firstOfWeekStr = (firstOfWeek+"").length==1?("0"+firstOfWeek):(firstOfWeek+"");
                searchConditions += "{name:create_date_datetime,condition: gt, value: '"+ (getCurrentMonth()+"-"+firstOfWeekStr) +"'},";
            }else if(thisQueryid == 4){	//本月新增
                searchConditions += "{name:create_date_datetime,condition: gt, value: '"+ (getCurrentMonth()+"-01") +"'},";
            }else{
                searchConditions += $("#"+searchFilter+" #queryExtParam_"+thisQueryid).text()+",";
            }
            var resultSearchConditions = searchConditions.substring(0,searchConditions.length-1);
            console.log("dinzhi_filter ==== "+resultSearchConditions);
            //设置定制查询过滤条件（肖功钦）
            if(ifSetConditions){//如果是点击定制查询则设置flexGrid过滤条件、如果是导出操作时则不设置flexGrid过滤条件
                //得到字段信息
                var fields = $(node).attr("fields");
                var mutilSearch = "["+resultSearchConditions+"]";
                //合并搜索条件
                var condition = {tableCode : tableCode,fields : fields,mutilSearch : mutilSearch};
                //重新刷新bootstrap表格
                flashBootstrapTable(bootstrapTable,condition);
            }
        }
    }
}

/**
 * 获得格式为 yyyy-mm-dd 的当前日期
 * @author  xiaogq
 */
function getCurrentDate(){
    var myDate = new Date();
    var currentYear = myDate.getFullYear();
    var currentMonth = ((myDate.getMonth()+1)+"").length==1?("0"+(myDate.getMonth()+1)):((myDate.getMonth()+1)+"");
    var currentDay = myDate.getDate();
    var currentDate = currentYear+"-"+currentMonth+"-"+currentDay;
    return currentDate;
}

/**
 * 获得格式为 yyyy-mm 的当前日期
 * @author  xiaogq
 */
function getCurrentMonth(){
    var myDate = new Date();
    var currentYear = myDate.getFullYear();
    var currentMonth = ((myDate.getMonth()+1)+"").length==1?("0"+(myDate.getMonth()+1)):((myDate.getMonth()+1)+"");
    var currentDate = currentYear+"-"+currentMonth;
    return currentDate;
}

/**
 * 得到每周的第一天(周日)
 * @author  mzh
 */
function getFirstDateOfWeek(theDate){
    var firstDateOfWeek;
    theDate.setDate(theDate.getDate() - theDate.getDay());
    firstDateOfWeek = theDate;
    return firstDateOfWeek;
}


